﻿using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using Xlit.Example.Business.Models;
using Xlit.Example.Business.Services;
using Xlit.Project.Mvc.Models;

namespace Xlit.Example.Controllers
{
    /// <summary>
    ///  系统用户管理
    /// </summary>
    [Route("api/user")]
    [ApiController]
    public class SysAccountController : Xlit.Project.Mvc.Controller.ControllerBase
    {
        /// <summary>
        ///  系统用户管理
        /// </summary>
        public SysAccountService SysAccountService { get; set; }

        /// <summary>
        ///  获取系统用户列表
        /// </summary>
        /// <param name="input">查询输入</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get([FromQuery] QueryInput input)
        {
            var output =  SysAccountService.Get(input);
            return Success(data: output); 
        }

        /// <summary>
        ///  获取系统用户的详细信息
        /// </summary>
        /// <param name="id">系统用户id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        //[Permission("系统账户.获取详情")]
        public IActionResult Get(int id)
        {
            var user =  SysAccountService.Get(id);
            var roles = ( SysAccountService
                    .GetUserRoleList(id))
                    .ToArray();
            var output = new SysAccountVO()
            {
                Id = user.Id,
                Username = user.Username,
                Name = user.Name,
                TelPhone = user.TelPhone,
                Email = user.Email,
                IsActive = user.IsActive,
                Roles = roles
            };
            return Success(data: output);
        }

        /// <summary>
        ///  新增/更新系统用户
        /// </summary>
        /// <param name="input">系统用户</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Post([FromBody] SysAccountVO input)
        {
             SysAccountService.Update(input);
            return Success();
        }

        /// <summary>
        ///  删除系统用户
        /// </summary>
        /// <param name="Id">系统用户id</param>
        /// <returns></returns>
        [HttpDelete("{Id}")]
        public IActionResult Delete(int Id)
        {
             SysAccountService.Delete(Id);
            return Success();
        }
    }
}